package com.team5101.lha.team5101.web.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.team5101.dto.AllOrderDto;
import com.team5101.dto.OrderDetailDto;
import com.team5101.dto.OrderDto;
import com.team5101.entity.TOrderDetail;
import com.team5101.shop.api.IOrderService;
import com.team5101.vo.AllOrderVo;
import com.team5101.vo.OrderDetailVo;
import com.team5101.vo.ResultBean;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/api/order")
public class OrderController {

      @Reference
     private IOrderService iOrderService;


      @PostMapping("/addOrder")
      public ResultBean addOrder(@RequestBody OrderDto orderDto){
          //String productInfos = orderDto.getProductInfos();
          orderDto.setProductInfo(orderDto.getProductInfo());
         /* ObjectMapper objectMapper = new ObjectMapper();
          JavaType jt =objectMapper.getTypeFactory().constructParametricType(ArrayList.class,TOrderDetail.class);
          try {
              List<TOrderDetail> orderDetails = objectMapper.readValue(productInfos, jt);
              orderDto.setProductInfo(orderDetails);
          } catch (JsonProcessingException e) {
              e.printStackTrace();
          }*/
          return iOrderService.createOrder(orderDto);
      }

      @GetMapping("/getAllOrder")
      public ResultBean getAllOrder(Integer uId){
          List<AllOrderDto> allOrder = iOrderService.getAllOrder(uId);
          System.out.println(allOrder);
          AllOrderVo allOrderVo = new AllOrderVo();
          allOrderVo.setoPsum(allOrder.get(0).getoPsum());
          allOrderVo.setoStatus(allOrder.get(0).getoStatus());
          List<String> oPimage = new ArrayList<>();
          for(AllOrderDto all:allOrder){
              oPimage.add(all.getoPimage());
          }
          allOrderVo.setoPimage(oPimage);
          return ResultBean.success(allOrderVo,"查询成功");
      }


      @GetMapping("/orderDetail")
     public ResultBean orderDetail(Integer oId){
      List<OrderDetailDto> orderDetail =iOrderService.queryOrderDetail(oId);
          OrderDetailVo od = new OrderDetailVo();
          od.setoAddr(orderDetail.get(0).getoAddr());
          od.setoCreattime(orderDetail.get(0).getoCreattime());
          od.setoEndtime(orderDetail.get(0).getoEndtime());
          od.setoMark(orderDetail.get(0).getoMark());
          od.setoOrdernum(orderDetail.get(0).getoOrdernum());
          od.setoPhone(orderDetail.get(0).getoPhone());
          od.setoPsum(orderDetail.get(0).getoPsum());
          od.setoReceive(orderDetail.get(0).getoReceive());
          od.setoStatus(orderDetail.get(0).getoStatus());
          List<TOrderDetail> proInfos = new ArrayList<>();
          for(OrderDetailDto o :orderDetail){
              proInfos.add(o.gettOrderDetail());
          }
          od.setProInfo(proInfos);
          return ResultBean.success(od,"查询成功");

      }
}
